package com.jamiexu.help.socket.utils;


import org.junit.Test;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class SocketUtils {

    @Test
    public void test() {
        System.out.println(("学号：0001\r\n" +
                "小明\r\n").getBytes(StandardCharsets.UTF_8).length);
        System.out.println(Arrays.toString("CONNECT_CONNECT".getBytes()));
    }



    //获取Socket传过来的数据
    public static byte[] getData(Socket socket) {
        try {
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            if (dataInputStream.available() == 0)
                return null;
            int len = dataInputStream.readInt();
            System.out.println("Accept length: " + len);
            byte[] data = new byte[len - 4];
            dataInputStream.readFully(data);
            return data;
        } catch (IOException e) {
            e.fillInStackTrace();
        }
        return null;
    }


    //把数据按照自己设置的数据结构进行分割成四个数据
    public static Object[] getBlock(byte[] src) {
        Object[] objects = new Object[4];
        try {
            DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(src));
            int len = dataInputStream.readInt();
            byte[] data = new byte[len];
            dataInputStream.read(data);
            objects[0] = new String(data, StandardCharsets.UTF_8);

            len = dataInputStream.readInt();
            data = new byte[len];
            dataInputStream.read(data);
            objects[1] = new String(data, StandardCharsets.UTF_8);

            len = dataInputStream.readInt();
            data = new byte[len];
            dataInputStream.read(data);
            objects[2] = data;

            objects[3] = dataInputStream.readInt();


            dataInputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


        return objects;

    }


    //发送指令
    public static void sendOPData(Socket socket, String opString, String headString, Object o) {
        try {
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            if (o != null) {
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                objectOutputStream.writeObject(o);
            }

            byte[] op = opString.getBytes(StandardCharsets.UTF_8);
            byte[] head = headString.getBytes(StandardCharsets.UTF_8);
            byte[] data = byteArrayOutputStream.toByteArray();
            int ver = 0;

            //数据报文总长度
            System.out.println("Send length:" + (op.length + head.length + byteArrayOutputStream.size() + 4 * 4 * 5));
            dataOutputStream.writeInt(op.length + head.length + byteArrayOutputStream.size() + 4 * 5);

//            //指令、报头、数据长度
            dataOutputStream.writeInt(op.length);
            for (byte b : op) {
                dataOutputStream.write(b);
                ver += b;
            }

            dataOutputStream.writeInt(head.length);
            for (byte b : head) {
                dataOutputStream.write(b);
                ver += b;
            }
//
            if (data.length != 0) {
                dataOutputStream.writeInt(data.length);
                for (byte b : data) {
                    dataOutputStream.write(b);
                    ver += b;
                }
            } else {
                dataOutputStream.writeInt(0);
            }

            dataOutputStream.writeInt(ver % 256);

            dataOutputStream.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    //发送文件块
    public static void sendFileData(Socket socket, String opString, String headString, byte[] data) {
        try {
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

            byte[] op = opString.getBytes(StandardCharsets.UTF_8);
            byte[] head = headString.getBytes(StandardCharsets.UTF_8);
            int ver = 0;

            //数据报文总长度
            System.out.println("Send length:" + (op.length + head.length + data.length + 4 * 4 * 5));
            dataOutputStream.writeInt(op.length + head.length + data.length + 4 * 5);

//            //指令、报头、数据长度
            dataOutputStream.writeInt(op.length);
            for (byte b : op) {
                dataOutputStream.write(b);
                ver += b;
            }

            dataOutputStream.writeInt(head.length);
            for (byte b : head) {
                dataOutputStream.write(b);
                ver += b;
            }
//
            dataOutputStream.writeInt(data.length);
            for (byte b : data) {
                dataOutputStream.write(b);
                ver += b;
            }

            dataOutputStream.writeInt(ver % 256);

            dataOutputStream.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
